<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>高阶函数--函数作为返回值</div>
    <script>
      //基本演示
      function makeFn() {
        let msg = "Hello function";
        return function () {
          console.log(msg);
        };
      }
      // 调用方法一
      let fn = makeFn();
      fn();

      //调用方法二
      makeFn()();

      //once
      function once(fn) {
        let done = false;
        return function () {
          if (!done) {
            done = true;
            // console.log(this);
            // console.log(arguments);
            // return fn.apply(this, arguments);
            return fn.apply(this, arguments);
          }
        };
      }
      let pay = once(function (money) {
        console.log(`支付了${money} RMB`);
      });

      pay(20);
      pay(20);
      pay(20);
      pay(25);
    </script>
  </body>
</html>
